import axios, { AxiosResData } from '@/utils/http';

// 后台定义的业务数据规范
export type ResData<T = any> = {
  code: number;
  msg: string;
  data: T;
  count?: number;
};

// type AxiosResData<T = any> = AxiosRes<ResData<T>>;

type Role = 'student' | 'admin' | 'super_admin';

export type UserInfo = {
  created: Date; // 时间
  name: string; // 学生花名
  vChat: string; // 微信名字
  phone: string; // 手机
  avatar: string; // 头像
  graduation_time: Date; // 毕业时间
  money: number; // 现在薪资
  role: Role; // 角色
  _id: string;
  has_person_info: boolean; // 是否填写个人信息
  topic_role: []; // 课程权限列表
  techStack: string; // 技术栈
};

export type MenuData = {
  hasMenu: boolean;
  key: string;
  label: string;
  path: string;
};

export type SubjectData = {
  title: string;
  value: string;
  children: SubjectData[];
  can_exam: boolean;
};

export type TopicData = {
  dec: string;
  title: string;
  two_id: string;
  _id: string;
  img: string[];
  answer: string;
  comment: string;
  pass: boolean;
  is_corret: boolean;
};

export type ExamData = {
  created: Date;
  is_judge: boolean;
  subject_name: string;
  topic_list: TopicData[];
  two_id: string;
  user_id: string;
  user_name: string;
  _id: string;
};

/*登录管理 */
// 退出登录
export function logout() {
  return new Promise(async (resolve) => {
    const res: AxiosResData = await axios.post('/api/user/logout');
    resolve(res.data.data);
  });
}
//登录
export type LoginBody = {
  phone: string;
  code: string;
};
export function login(body: LoginBody) {
  return new Promise(async (resolve) => {
    const res: AxiosResData<UserInfo> = await axios.post('/api/user/login', body);
    resolve(res.data.data);
  });
}

/*用户管理 */

//获取用户信息
export function getUserInfoRequest() {
  return new Promise(async (resolve) => {
    const res: AxiosResData<UserInfo> = await axios.get('/api/user');
    resolve(res.data.data || {});
  });
}
